<template>
  <a-row :gutter="24">
    <a-col :md="4" :sm="24">
      <a-card :bordered="false" :loading="treeLoading">
        <a-directory-tree
          v-if="treeList.length"
          :showIcon="false"
          :treeData="treeList"
          @select="handleClick"
          :defaultExpandAll="true"
          :replaceFields="replaceFields"
          style="overflow: unset"
        />
      </a-card>
    </a-col>

    <a-col :md="19" :sm="24">
      <a-card :bordered="false">

        <div class="table-page-search-wrapper">
          <a-form layout="inline">
            <a-row :gutter="0">

              <a-col :md="6" :sm="24">
                <a-form-item label="网关名称">
                  <a-input v-model="queryParam.gatewayName" allow-clear placeholder="请输入网关名称"/>
                </a-form-item>
              </a-col>

              <a-col :md="!advanced && 8 || 24" :sm="24">
                <span
                  class="table-page-search-submitButtons"
                  style="padding-left: 16px;"
                  :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
                  <a-button type="primary" class="btn_search_reset" @click="$refs.table.refresh(true)"><a-icon
                    type="search"/>查询</a-button>
                </span>
              </a-col>
              <a-col :md="!advanced && 10 || 24" :sm="24">
                <span
                  class="table-page-search-submitButtons"
                  style="float: right;">
                  <a-button
                    class="btn_add"
                    type="primary"
                    @click="handleBatchDataReportStatus(1)">一键开启数据上报</a-button>
                  <a-button
                    class="btn_add"
                    type="primary"
                    @click="handleBatchDataReportStatus(0)">一键关闭数据上报</a-button>
                </span>
              </a-col>
            </a-row>
          </a-form>
        </div>

        <s-table
          ref="table"
          size="default"
          :columns="columns"
          :data="loadData"
          :alert="true"
          :rowKey="(record) => record.id"
        >

          <span slot="status" slot-scope="text">
            {{ getStatus(text) }}
          </span>

          <span slot="onlineStatus" slot-scope="text">
            {{ getOnlineStatus(text) }}
          </span>

          <span slot="dataReportingSwitch" slot-scope="text, record">
            <a-popconfirm placement="top" :title="text===0? '确定打开？':'确定关闭？'" @confirm="() => changeSwitch(record)">
              <a>{{ text === 1? '开':'关' }}</a>
            </a-popconfirm>
          </span>

          <span slot="action" slot-scope="text, record">
            <a @click="$refs.editForm.edit(record)" class="btn_edit">编辑</a>

            <a-divider type="vertical"/>
            <a-dropdown>
              <a class="btn_more"> 更多 <a-icon type="down"/></a>
              <a-menu slot="overlay">
                <a-menu-item>
                  <a-popconfirm placement="topRight" title="确认删除？" @confirm="() => del(record)">
                    <a>删除</a>
                  </a-popconfirm>
                  <a @click="$refs.collectorIndexForm.toIndex(record)">参数配置</a>
                  <a @click="$refs.channelIndexForm.toIndex(record)">通道设置</a>
                  <a @click="$refs.gatewayConfigForm.toIndex(record)">网关配置</a>
                  <a @click="$refs.remoteConfigForm.toIndex(record)">远程配置</a>
                </a-menu-item>
              </a-menu>
            </a-dropdown>

          </span>
        </s-table>
        <add-form ref="addForm" @ok="handleOk"/>
        <edit-form ref="editForm" @ok="handleOk"/>
        <channel-index-form ref="channelIndexForm" @ok="handleOk"/>
        <collector-index-form ref="collectorIndexForm" @ok="handleOk"/>
        <gateway-config-form ref="gatewayConfigForm" @ok="handleOk"/>
        <remote-config-form ref="remoteConfigForm" @ok="handleOk"/>
        <import-form ref="importForm" @ok="handleOk"/>
      </a-card>
    </a-col>
  </a-row>
</template>

<script>
  import { STable } from '@/components'
  import {
    page,
    del,
    selfTest,
    changeSwitch,
    batchChangeDataReportStatus
  } from '@/api/modular/main/bizgateway/bizGatewayManage'
  import addForm from './addForm'
  import editForm from './editForm'
  import importForm from './importForm'
  import { Empty } from 'ant-design-vue'
  import { treeList } from '@/api/modular/main/bizstation/bizStationManage'
  import channelIndexForm from './bizgatewaychannel/index'
  import collectorIndexForm from '@/views/main/bizcollectorconfig/index'
  import gatewayConfigForm from '../bizgatewayconfigitem/index'
  import remoteConfigForm from '../bizremoteconfigitem/index'

  export default {
    components: {
      STable,
      addForm,
      importForm,
      editForm,
      channelIndexForm,
      collectorIndexForm,
      gatewayConfigForm,
      remoteConfigForm
    },

    data () {
      return {
        advanced: false, // 高级搜索 展开/关闭
        queryParam: {}, // 查询参数
        columns: [ // 表头
          {
            title: '网关名称',
            dataIndex: 'gatewayName'
          },
          {
            title: '网关状态',
            dataIndex: 'status',
            scopedSlots: { customRender: 'status' },
            align: 'center'
          },
          {
            title: '运行状态',
            dataIndex: 'onlineStatus',
            scopedSlots: { customRender: 'onlineStatus' },
            align: 'center'
          },
          {
            title: '设备IP',
            dataIndex: 'gatewayIp'
          },
          {
            title: '数据上报开关',
            dataIndex: 'dataReportingSwitch',
            scopedSlots: { customRender: 'dataReportingSwitch' },
            align: 'center'
          },
          {
            title: '创建日期',
            dataIndex: 'createdAt',
            width: 150,
            align: 'center'
          },
          {
            title: '操作',
            dataIndex: 'action',
            scopedSlots: { customRender: 'action' },
            width: 250,
            align: 'center'
          }
        ],
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          return page(Object.assign(parameter, this.queryParam)).then((res) => {
            return res.data
          })
        },
        treeList: [],
        gatewayLoading: true,
        simpleImage: Empty.PRESENTED_IMAGE_SIMPLE,
        selectedRowKeys: [],
        selectedRows: [],
        defaultExpandedKeys: [],
        replaceFields: {
          key: 'id'
        },
        treeLoading: true,
        stationId: '',
        btnLoading: false
      }
    },

    created () {
      this.getStationTree()
    },

    methods: {
      del (record) {
        del({
          id: record.id
        }).then((res) => {
          if (res.success) {
            this.$message.success('删除成功')
            this.$refs.table.refresh()
          } else {
            this.$message.error('删除失败：' + res.message)
          }
        }).catch((err) => {
          this.$message.error('删除错误：' + err.message)
        })
      },
      getStatus (text) {
        if (text === 1) {
          return '开启'
        } else {
          return '禁用'
        }
      },
      getOnlineStatus (text) {
        if (text === 1) {
          return '在线'
        } else {
          return '离线'
        }
      },
      getStationTree () {
        treeList(Object.assign(this.queryParam)).then(res => {
          this.treeLoading = false
          if (!res.success) {
            return
          }
          this.treeList = res.data
        })
      },
      changeSwitch (record) {
        if (record.dataReportingSwitch === 1) record.dataReportingSwitch = 0
        else record.dataReportingSwitch = 1
        changeSwitch({ id: record.id, dataReportingSwitch: record.dataReportingSwitch }).then(res => {
          if (res.success) {
            this.$message.success('操作成功')
            this.$refs.table.refresh()
          } else {
            this.$message.error('操作失败：' + res.message)
          }
        })
      },
      // 一键开启或关闭数据上报开关
      handleBatchDataReportStatus (status) {
        if (this.stationId === '') {
          this.$message.error('请选择站点！')
          return
        }
        const content = status === 1 ? '确定一键开启站点下所有网关的数据上报？' : '确定一键关闭站点下所有网关的数据上报？'
        const param = {
          dataReportingSwitch: status,
          stationId: this.stationId
        }
        this.$confirm({
          title: '提示',
          content: content,
          okText: '确定',
          cancelText: '取消',
          onOk: () => {
            batchChangeDataReportStatus(param).then(res => {
              if (res.success) {
                this.$message.success('操作成功')
                this.$refs.table.refresh()
              } else {
                this.$message.error('操作失败：' + res.message)
              }
            })
          },
          onCancel () {
          }
        })
      },
      toggleAdvanced () {
        this.advanced = !this.advanced
      },
      handleOk () {
        this.$refs.table.refresh()
      },
      onSelectChange (selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
      },
      handleClick (e) {
        this.stationId = e.toString()
        this.queryParam.stationId = e.toString()
        this.handleOk()
      },
      selfTest (record) {
        this.btnLoading = true
        selfTest({ ip: record.gatewayIp }).then(res => {
          this.btnLoading = false
          if (res.success) {
            this.$message.success(res.data)
          } else {
            this.$message.error(res.message)
          }
        })
      },
      isShow () {
        if (this.stationId) {
          return true
        }
        return false
      }
    }
  }
</script>

<style lang="less" scoped>
  /*@import '../~@/assets/css/list.less';*/
  .table-operator {
    margin-bottom: 18px;
  }

  button {
    margin-right: 8px;
  }

  .ant-tree li {
    overflow: no-content !important;
    white-space: pre-wrap !important;
  }
  .table-page-search-wrapper{
    padding-left: 10px !important;
    padding-right: 10px;
    .ant-row{
      margin-left: 0px !important;
    }
  }
</style>
